﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SuperSnake
{
    public partial class JxSnakeFrm : Form
    {
        public JxSnakeFrm()
        {
            InitializeComponent();
        }
        private Snake mySnake = new Snake(); //蛇实体
        private bool show = true; //食物闪烁标志
        private direction directNow; //当前运动方向
        private bool isnew = true;//区别重新开始和暂停后开始
        private bool gameOver = false;//游戏结束标志
        public enum direction
        {
            up=1,
            down=2,
            left=3,
            right=4
        };
        private void Form1_Load(object sender, EventArgs e)
        {
            GameField.Width = 25;
            GameField.Height = 21;
            GameField.winHandle = pictureBox1.Handle;
            GameField.backColor = pictureBox1.BackColor;
            
        }
        /*键盘事件*/
        protected override bool ProcessDialogKey(Keys keyData)
        {
            
            switch (keyData)
            {
                case Keys.Up:
                    if (directNow == direction.down||gameOver)//不逆行
                        break;
                    if (!mySnake.up()) GameOver();
                    directNow = direction.up;
                    break;
                case Keys.Left:
                    if (directNow == direction.right || gameOver)//不逆行
                        break;
                    if (!mySnake.left()) GameOver();
                    directNow = direction.left;
                    break;
                case Keys.Right:
                    if (directNow == direction.left || gameOver)//不逆行
                        break;
                    if (!mySnake.right()) GameOver();
                    directNow = direction.right;
                    break;
                case Keys.Down:
                    if (directNow == direction.up || gameOver)//不逆行
                        break;
                    if (!mySnake.down()) GameOver();
                    directNow = direction.down;
                    break;
            }      
            if (keyData == Keys.Up || keyData == Keys.Down || keyData == Keys.Left || keyData == Keys.Right)
                return false;
            else
                return base.ProcessDialogKey(keyData);
        }
        /*游戏结束事件*/
        public void GameOver()
        {
            timer1.Stop();
            timer2.Stop();
            pictureBox1.Refresh();
            isnew = true;
            gameOver = true;
            pictureBox1.BackgroundImage = Properties.Resources.gameover;
            MessageBox.Show("游戏结束！");
        }
        /*食物闪动时钟*/
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (GameField.foodVisible)
            {
                if (show)
                {
                    GameField.food.Erase(GameField.winHandle);
                    show = !show;
                }
                else
                {
                    GameField.food.Draw(GameField.winHandle);
                    show = !show;
                }
            }
            else
                GameField.food.Erase(GameField.winHandle);

        }
        /*蛇移动时钟*/
        private void timer1_Tick(object sender, EventArgs e)
        {
            switch (directNow)
            {
                case direction.up: if (!mySnake.up()) GameOver();
                    break;
                case direction.left: if (!mySnake.left()) GameOver();
                    break;
                case direction.right: if (!mySnake.right()) GameOver();
                    break;
                case direction.down: if (!mySnake.down()) GameOver();
                    break;
            }
        }
         #region 游戏菜单……
        private void 开始ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (isnew)
            {
                pictureBox1.Refresh();
                pictureBox1.BackgroundImage = null;
                mySnake = new Snake(); //蛇实体
                mySnake.Draw(GameField.winHandle);
                directNow = direction.right;
                show = true; //食物闪烁标志
                gameOver = false;
                GameField.createFood(mySnake);
                GameField.foodVisible = true;
            }
            timer2.Start();
            timer1.Start();
            开始ToolStripMenuItem.Enabled = false;
            暂停ToolStripMenuItem.Enabled = true ;
            结束ToolStripMenuItem.Enabled = true;
        }
        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void 暂停ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            timer2.Stop();
            isnew = false;
            开始ToolStripMenuItem.Enabled = true;
            暂停ToolStripMenuItem.Enabled = false;
            结束ToolStripMenuItem.Enabled = true;
        }
        private void 结束ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            timer2.Stop();
            pictureBox1.Refresh();
            isnew = true;
            gameOver = true;
            pictureBox1.BackgroundImage = Properties.Resources.gameover;
            暂停ToolStripMenuItem.Enabled = false;
            结束ToolStripMenuItem.Enabled = false;
            开始ToolStripMenuItem.Enabled = true;
        }
        private void 较低ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            timer1.Interval = 500;
            较低ToolStripMenuItem.Checked = true;
            低ToolStripMenuItem.Checked = false;
            中ToolStripMenuItem.Checked = false;
            高ToolStripMenuItem.Checked = false;
            较高ToolStripMenuItem.Checked = false;
        }

        private void 低ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            timer1.Interval = 450;
            较低ToolStripMenuItem.Checked = false;
            低ToolStripMenuItem.Checked = true;
            中ToolStripMenuItem.Checked = false;
            高ToolStripMenuItem.Checked = false;
            较高ToolStripMenuItem.Checked = false;
        }

        private void 中ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            timer1.Interval = 350;
            较低ToolStripMenuItem.Checked = false;
            低ToolStripMenuItem.Checked = false;
            中ToolStripMenuItem.Checked = true;
            高ToolStripMenuItem.Checked = false;
            较高ToolStripMenuItem.Checked = false;
        }

        private void 高ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            timer1.Interval = 250;
            较低ToolStripMenuItem.Checked = false;
            低ToolStripMenuItem.Checked = false;
            中ToolStripMenuItem.Checked = false;
            高ToolStripMenuItem.Checked = true;
            较高ToolStripMenuItem.Checked = false;
        }

        private void 较高ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            timer1.Interval = 150;
            较低ToolStripMenuItem.Checked = false;
            低ToolStripMenuItem.Checked = false;
            中ToolStripMenuItem.Checked = false;
            高ToolStripMenuItem.Checked = false;
            较高ToolStripMenuItem.Checked = true;
        }
        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            about ab = new about();
            ab.Show();
        }
#endregion
    }
}